package com.gks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gks.entities.Article;
import com.gks.entities.vo.ArticleVo;
import com.gks.mapper.ArticleMapper;
import com.gks.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Void addArticle(Article article) {
        try {
            article.setCreateTime(new Date());
            System.out.println("Inserting article: " + article);
            int result = articleMapper.insert(article);
            if (result <= 0) {
                throw new RuntimeException("添加文章失败");
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加文章失败：" + e.getMessage());
        }
    }

    @Override
    public Void updateArticle(Article article) {
        try {
            // 检查文章是否存在
            Article existingArticle = articleMapper.selectById(article.getId());
            if (existingArticle == null) {
                throw new RuntimeException("文章不存在");
            }
            
            // 保持创建时间不变
            article.setCreateTime(existingArticle.getCreateTime());
            
            int result = articleMapper.updateById(article);
            if (result <= 0) {
                throw new RuntimeException("更新文章失败");
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新文章失败：" + e.getMessage());
        }
    }

    @Override
    public Void deleteArticle(Integer id) {
        try {
            // 检查文章是否存在
            Article existingArticle = articleMapper.selectById(id);
            if (existingArticle == null) {
                throw new RuntimeException("文章不存在");
            }
            
            int result = articleMapper.deleteById(id);
            if (result <= 0) {
                throw new RuntimeException("删除文章失败");
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除文章失败：" + e.getMessage());
        }
    }

    @Override
    public ArticleVo selectArticle(Integer id) {
        try {
            Article article = articleMapper.selectById(id);
            if (article == null) {
                return null;
            }
            ArticleVo vo = new ArticleVo();
            BeanUtils.copyProperties(article, vo);
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询文章失败：" + e.getMessage());
        }
    }

    @Override
    public List<ArticleVo> searchArticles(String title, String category) {
        try {
            LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
            
            if (title != null && !title.isEmpty()) {
                wrapper.like(Article::getTitle, title);
            }
            if (category != null && !category.isEmpty()) {
                wrapper.eq(Article::getCategory, category);
            }
            
            wrapper.orderByDesc(Article::getCreateTime);
            
            List<Article> articles = articleMapper.selectList(wrapper);
            return articles.stream()
                .map(article -> {
                    ArticleVo vo = new ArticleVo();
                    BeanUtils.copyProperties(article, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询文章列表失败：" + e.getMessage());
        }
    }
}
